package com.xyy.saas.payment.config;

import io.buji.pac4j.realm.Pac4jRealm;
import io.buji.pac4j.subject.Pac4jPrincipal;
import io.buji.pac4j.token.Pac4jToken;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.pac4j.core.profile.CommonProfile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.util.List;


/**
 * @author wangtianqi
 * @date 2020/5/11 13:36
 */
@Repository
public class SsoClientConfiguration extends Pac4jRealm {
    private static final Logger logger= LoggerFactory.getLogger(SsoClientConfiguration .class);

    /**
     *认证
     *
     *@param authenticationToken
     *@return
     *@throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken
                                                               authenticationToken)throws AuthenticationException{
        final Pac4jToken pac4jToken =(Pac4jToken)authenticationToken;
        final List<CommonProfile> commonProfileList=pac4jToken.getProfiles();
        final CommonProfile commonProfile=commonProfileList.get(0);
        logger.info("sso server response:"+commonProfile.toString());
        final Pac4jPrincipal principal=new
                Pac4jPrincipal(commonProfileList,getPrincipalNameAttribute());
        final PrincipalCollection principalCollection=new
                SimplePrincipalCollection(principal,getName());
        return new SimpleAuthenticationInfo(principalCollection,commonProfileList.hashCode());
    }


}
